package cn.swust.designpatternlearningplatform.dao;


import cn.swust.designpatternlearningplatform.dao.sql.TopicDaoSql;
import cn.swust.designpatternlearningplatform.domain.bo.TopicDifficultyBo;
import cn.swust.designpatternlearningplatform.domain.dto.TopicDto;
import cn.swust.designpatternlearningplatform.domain.entity.Topic;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;

@Mapper
public interface TopicDao {
    /**
     * 更新或者插入数据
     *
     * @param topic
     * @return java.lang.Integer
     * @author Tang
     * @date 2023/5/13 17:31
     **/
    @Insert({"Insert Into topic(topic_id,topic_type, topic_content, topic_content_path, topic_answer, topic_analyse, topic_analyse_path, topic_difficulty) " +
            "VALUE(#{topic.topicId},#{topic.topicType}," +
            "#{topic.topicContent},#{topic.topicContentPath}," +
            "#{topic.topicAnswer},#{topic.topicAnalyse}," +
            "#{topic.topicAnalysePath},#{topic.topicDifficulty}) " +
            "ON DUPLICATE KEY UPDATE topic_type = VALUES(topic_type),\n" +
            "                        topic_content = VALUES(topic_content),\n" +
            "                        topic_content_path = VALUES(topic_content_path),\n" +
            "                        topic_answer = VALUES(topic_answer),\n" +
            "                        topic_analyse = VALUES(topic_analyse),\n" +
            "                        topic_analyse_path = VALUES(topic_analyse_path),\n" +
            "                        topic_difficulty = VALUES(topic_difficulty)"})
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "topic.topicId", before = false, resultType = Integer.class, keyColumn = "topic_id")
    Integer insertOrUpdateTopic(@Param("topic") Topic topic);

    /**
     * 根据题目id查询题目信息
     *
     * @param topicId 题目id信息
     * @return cn.swust.designpatternlearningplatform.domain.entity.Topic
     * @author Tang
     * @date 2023/4/16 0:38
     **/
    @Results(value = {
            @Result(property = "topicId", column = "topic_id"),
            @Result(property = "topicType", column = "topic_type"),
            @Result(property = "topicContent", column = "topic_content"),
            @Result(property = "topicContentPath", column = "topic_content_path"),
            @Result(property = "topicAnswer", column = "topic_answer"),
            @Result(property = "topicAnalyse", column = "topic_analyse"),
            @Result(property = "topicAnalysePath", column = "topic_analyse_path"),
            @Result(property = "topicDifficulty", column = "topic_difficulty"),

    })
    @SelectProvider(type = TopicDaoSql.class, method = "selectTopicById")
    Topic selectTopicById(Integer topicId);

    @SelectProvider(type = TopicDaoSql.class, method = "selectTopicsByIds")
    List<Topic> selectTopicsByIds(@Param("ids") List<Integer> topicIds);

    /**
     * @param :
     * @return List<TopicDifficultyBo>
     * @author wang
     * description 获取题目里面的题目id和困难程度
     * date 2023/5/19 12:14
     */
    @Results(value = {
            @Result(property = "topicId", column = "topic_id"),
            @Result(property = "topicDifficulty", column = "topic_difficulty"),
    })
    @SelectProvider(type = TopicDaoSql.class, method = "getTopicAndDifficulty")
    List<TopicDifficultyBo> getTopicAndDifficulty();

    /**
     * @param :
     * @return List<Topic>
     * @author wang
     * description 获取全部试题
     * date 2023/5/19 12:15
     */
    @Results(value = {
            @Result(property = "topicId", column = "topic_id"),
            @Result(property = "topicDifficulty", column = "topic_difficulty"),
            @Result(property = "topicType", column = "topic_type"),
            @Result(property = "topicContent", column = "topic_content"),
            @Result(property = "topicContentPath", column = "topic__content_path"),
            @Result(property = "topicAnswer", column = "topic_answer"),
            @Result(property = "topicAnalyse", column = "topic_analyse"),
            @Result(property = "topicAnalysePath", column = "topic_analyse_path"),
    })
    @SelectProvider(type = TopicDaoSql.class, method = "getAllTopic")
    List<Topic> getAllTopic();

    /**
     * @param topicId:
     * @return String
     * @author wang
     * description 通过题目id查找题目的难度类型
     * date 2023/7/2 19:31
     */
    @SelectProvider(type = TopicDaoSql.class, method = "getTopicDifficultyById")
    String getTopicDifficultyById(Integer topicId);

    /**
     * @param topicId:
     * @return String
     * @author wang
     * description 通过题目id获取对应题目的答案
     * date 2023/5/19 12:16
     */
    @SelectProvider(type = TopicDaoSql.class, method = "getAnswer")
    String getAnswer(Integer topicId);

    /**
     * 根据题目类型查询对应的题目id
     *
     * @return java.util.List<java.lang.Integer>
     * @author Tang
     * @date 2023/5/16 17:10
     **/
    @Select("SELECT topic_id " +
            "FROM topic " +
            "WHERE topic_type = #{type}")
    List<Integer> selectTopicIdByTopicType(@Param("type") String type);

    /**
     * 根据内容分页查询题目id
     *
     * @param currentPage
     * @param pageSize
     * @return java.util.List<java.lang.Integer>
     * @author Tang
     * @date 2023/5/19 20:26
     **/
    @Select("SELECT topic_id\n" +
            "FROM topic\n" +
            "LIMIT #{current},#{pageSize}")
    List<Integer> selectTopicIdByPage(@Param("current") Integer currentPage,
                                      @Param("pageSize") Integer pageSize);

    /**
     * 分页查询所有数据
     *
     * @return java.lang.Integer
     * @author Tang
     * @date 2023/5/19 20:30
     **/
    @Select("SELECT COUNT(*) " +
            "FROM topic ")
    Integer selectTopicByPageAllNum();

    /**
     * 根据id列表进行查询
     */
    List<Topic> getTopicsByIds(@Param("topicIds") Collection<Integer> topicIds);

    /**
     * 更新题目
     *
     * @param topic
     * @return void
     * @author Tang
     * @date 2023/7/22 10:32
     **/
    void update(@Param("topic") Topic topic);

    /**
     * 根据topicId删除题目
     *
     * @param topicId
     * @return void
     * @author Tang
     * @date 2023/7/22 11:38
     **/
    void deleteByTopicId(@Param("id") Integer topicId);

    /**
     * 根据topicIds批量查询带有知识点信息的题目信息
     *
     * @param topicIds
     * @return java.util.List<cn.swust.designpatternlearningplatform.domain.dto.TopicDto>
     * @author Tang
     * @date 2023/10/12 16:42
     **/
    List<TopicDto> selectTopicWithPointInfo(@Param("topicIds") Collection<Integer> topicIds);

    /**
     * 分页查询知识点下的题目信息
     *
     * @param pointIds
     * @param current
     * @param size
     * @return java.util.List<java.lang.Integer>
     * @author Tang
     * @date 2023/10/12 16:43
     **/
    List<Integer> selectTopicIdsByPointIds(@Param("pointIds") Collection<Integer> pointIds, @Param("current") Integer current, @Param("pageSize") Integer size);
}
